import Cookies from 'js-cookie'

const TokenKey = 'Admin-Token'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

// 通用存储
export function setStorage(k, data) {
  const key = k
  if (typeof data == "object") {
    localStorage.setItem(key, `object:&&:${JSON.stringify(data)}`);
  } else if (typeof data == "string") {
    localStorage.setItem(key, `string:&&:${data}`);
  } else if (typeof data == "number") {
    localStorage.setItem(key, `number:&&:${data}`);
  } else if (typeof data == "function") {
    console.log("方法函数暂时先不做处理");
  } else {
    console.log("遇到特殊的数据类型在单独处理");
  }
}
// 通用获取
export function getStorage(k) {
  const key = k
  const _data = localStorage.getItem(key);
  if (_data) {
    const [stringType, stringVal] = _data.split(":&&:");
    if (stringType == "object") {
      return JSON.parse(stringVal);
    } else if (stringType == "string") {
      return stringVal;
    } else if (stringType == "number") {
      return Number(stringVal);
    } else {
      return "未知数据";
    }
  } else {
    console.log("未知数据");
    return null;
  }
}
// 通用删除
export function removeStorage(k) {
  const key = k
  localStorage.removeItem(key);
}
